# -*- coding: utf-8 -*-

import sys, os
import glob

def print_file_ver(filename):
    with open(filename) as f:
        count = 0
        for line in f:
            if line.startswith('/*major') or line.startswith('/*- major'):
                count = 3
            if count > 0:
                print line.split()[-1],
                count -= 1
                if count == 0: print "  ",os.path.basename(filename)

def check_dir(dirpath):
    ret = os.walk(dirpath)
    for root, dirs, files in ret:
        pdir = os.path.split(root)[-1]
        if pdir == 'trunk' and 'CMakeLists.txt' in files and 'test' in dirs:
            #print root, files
            for item in files:
                if len(item)>=5 and item[0:4] == 'ver_':
                    print_file_ver(root+os.sep+item)

        if 'main' in dirs and 'CMakeLists.txt' in files and 'test' in dirs :
            nret = glob.glob(root+'\\main\\ver_*.c')
            for verf in nret:
                print_file_ver(verf)

def print_help():
    print "checkver.py [dir [dir]]"

if __name__ == '__main__':
    if len(sys.argv) == 1:
        print_help()
    else:
        for d in sys.argv[1:]:
            check_dir(d)
